package demo.kuaisu;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {2,5,1,6,9,10,4,23,15};
        method(arr,0,arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
            //
        }
    }
    private static void method(int[] arr, int left, int right) {
        if(right < left){
            return;
        }
        //定义最左侧的索引为left
        int left0 = left;
        //定义最右侧的索引为right
        int right0 = right;
        //定义最左侧的数为基准数
        int baseNumber = arr[left0];
        while(left != right){
            //从数组最右侧开始查找比基准数小的数字
            while(arr[right] >= baseNumber && right > left){
                right--;
            }
            //从数组最左侧查找比基准数大的数字
            while(arr[left] <= baseNumber && right > left){
                left++;
            }
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
        //基准数归位
        int temp = arr[left];
        arr[left] = arr[left0];
        arr[left0] = temp;

        method(arr,left0,left-1);
        method(arr,left +1,right0);
    }

}
